package com.lpf;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

import java.io.IOException;

/**
 * 判断某个 Document 是否存在
 */
public class Main04 {

    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http"),
                        new HttpHost("localhost", 9200, "http")
                )
        );

        GetRequest request = new GetRequest("skywalking", "type", "1");

        // 不查询 "_source"字段以及其他字段
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");

        // 通过exists()方法发送 GetRequest
        boolean exists = client.exists(request);
        System.out.println(exists);
    }
}